<!DOCTYPE html>
<html>
<head>
    <title>部门管理</title>
    <#include "/common/import.ftl" >
    <script>
        var orgId = '${org.orgId!""}';
        $(function () {
            $('#deptList').treegrid({
                title: '部门列表',
                iconCls: 'icon-ok',
                fitColumns: true,
                nowrap: false,
                rownumbers: true,
                animate: true,
                collapsible: false,
                url: 'dept!data.action?id=${id!""}' + '&orgId=' + orgId,
                idField: 'deptId',
                treeField: 'deptCname',
                frozenColumns: [[
                    {
                        title: '部门名称', field: 'deptCname', width: 150,
                        formatter: function (value) {
                            return '<span style="color:blue">' + value + '</span>';
                        }
                    }
                ]],
                columns: [[
                    {field: 'deptEname', title: '部门标识', width: 150},
                    {field: 'fullEname', title: '全标识', width: 150},
                    {field: 'orgEname', title: '所属机构', width: 150},
                    {field: 'createTime', title: '创建时间', width: 150},
                    //{field:'deptStatus',title:'状态',width:80},
                    {field: 'deptRemark', title: '部门经理', width: 150}

                ]],

                toolbar: [
                    {
                        text: '增加一级部门',
                        iconCls: 'icon-add',
                        handler: function () {
                            saveDept('add');
                        }

                    }, '-',
                    {
                        text: '增加子部门',
                        iconCls: 'icon-add',
                        handler: function () {
                            saveDept('sub');
                        }

                    }, '-',
                    {
                        text: '删除',
                        iconCls: 'icon-cut',
                        handler: function () {
                            deleteDept();
                        }
                    }, '-',
                    {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function () {
                            saveDept('edit');
                        }
                    }, '-',
                    {
                        text: '部门人员',
                        iconCls: 'icon-search',
                        handler: function () {
                            deptUsers();
                        }
                    }, '-',
                    {
                        text: '刷新',
                        iconCls: 'icon-reload',
                        handler: function () {
                            reload();
                        }
                    }, '-',
                    {
                        text: '权限设置',
                        handler: function () {
                            actSeting();
                        }
                    }
                ],

                onBeforeLoad: function (row, param) {
                    if (row) {
                        $(this).treegrid('options').url = 'dept!data.action?' + '&orgId=' + orgId;
                    }
                },
                onDblClickRow: function (row) {
                    saveDept('edit');
                }
            });
        });


        function saveDept(paraFlag) {
            $('#deptAddFrame').html('');
            var deptId = '0';
            var url = 'add.action?orgId=${org.orgId!""}&orgEname=${org.orgEname!""}&fatherId=';

            if ('edit' == paraFlag) {
                var node = getSelected();
                if (node) {
                    deptId = node.deptId;
                    url = 'add.action?deptId=' + deptId;
                    $('#deptAddFrame').attr("src", url);
                    $('#deptAddWin').window('open');
                }
            }
            if ('add' == paraFlag) {
                alert(url);
                $('#deptAddFrame').attr("src", url);
                $('#deptAddWin').window('open');
            }
            if ('sub' == paraFlag) {
                var node = getSelected();
                if (node) {
                    fatherDeptId = node.deptId;
                    url = url + fatherDeptId;
                    $('#deptAddFrame').attr("src", url);
                    $('#deptAddWin').window('open');
                }
            }

        }

        function deleteDept() {
            var node = getSelected();
            if (node) {
                $.messager.confirm('确认', '您确定要删除:<font color="red">' + node.deptCname + '</font> ？', function (r) {
                    if (r) {
                        $.ajax({
                            type: "post",
                            url: "del.action?id=" + node.deptId,
                            success: function (data, textStatus) {
                                var result = jQuery.parseJSON(data);
                                if (result.flag == 'T') {
                                    $.messager.alert('结果', '操作成功', 'info');
                                    $('#deptList').treegrid('remove', node.deptId);
                                    if ("0" == node.fatherId) {
                                        location.reload();
                                    } else {
                                        reloadTree(node.fatherId, '');
                                    }
                                } else {
                                    $.messager.alert('提交结果', "操作失败：" + result.msg, 'error');
                                }
                            }
                        });
                    }
                });
            }
        }

        var widthcount = $(document.body).width() - 20;
        var leftcount = document.documentElement.scrollLeft;

        /**
         *部门下用户列表
         */
        function deptUsers() {
            var node = getSelected();
            if (node) {
                deptId = node.deptId;
                url = '${ctx}/uct/dept/dept-users.action?deptId=' + deptId;
                if ($.isFunction(window.parent.addTab)) {
                    window.parent.addTab('部门用户列表[' + node.deptCname + ']', url);
                } else {
                    $('#deptActWin').css({
                        "width": widthcount,
                        "height": "450px",
                        "left": leftcount
                    });
                    $('#orgDeptActFrame').attr("src", url);
                    $('#deptActWin').panel('setTitle', '部门用户列表[' + node.deptCname + ']');
                    $('#deptActWin').window('open');
                }
            }
        }

        function getSelected() {
            var selected = $('#deptList').treegrid('getSelected');
            if (selected) {
                return selected;
            } else {
                $.messager.alert('提示', '请选择要操作的数据', 'info');
            }
        }

        function reload() {
            var node = $('#deptList').treegrid('getSelected');
            if (node) {
                $('#deptList').treegrid('reload', node.deptId);
            } else {
                $('#deptList').treegrid('reload');
            }
        }

        function reloadTree(fatherId, nodeId) {
            if (nodeId == "") {
                var parent = $('#deptList').treegrid('getParent', fatherId);
                if (parent) {
                    var parentId = parent["deptId"];
                    $('#deptList').treegrid('reload', parentId);
                } else {
                    $('#deptList').treegrid('reload');
                }
            } else {
                $('#deptList').treegrid('reload', fatherId);
            }
        }

        //设置权限
        function actSeting() {
            var node = getSelected();
            if (node) {
                var deptId = node.deptId;
                $.ajax({
                    type: "post",
                    url: '${ctx}/uct/dept/dept-act!checkAct.action?',
                    success: function (data, textStatus) {
                        var result = jQuery.parseJSON(data);
                        if (result.flag == 'T') {
                            if ("1" != "${sysUser}" && node.deptId == "${loginUser.user.deptId!''}") {
                                $.messager.alert('提交结果', "不能给自己部门授权", 'info');
                            } else {
                                var url = '${ctx}/uct/dept/dept-act.action?deptId=' + deptId;
                                $('#deptActWin').css({
                                    "width": widthcount,
                                    "height": "450px",
                                    "left": leftcount
                                });
                                $('#orgDeptActFrame').attr("src", url);
                                $('#deptActWin').panel('setTitle', '机构权限[<font color=red>' + node.deptCname + '</font>]');
                                $('#deptActWin').window('open');
                            }
                        } else {
                            $.messager.alert('提交结果', result.msg, 'error');
                        }
                    }
                });
            }
        }

        function closeDeptWin() {
            $("#deptAddWin").window('close');
        }

    </script>
</head>

<body>

<table id="deptList"></table>


<div id="deptAddWin" class="easyui-window" title="部门信息" style="padding:5px;width: 550px;height:450;"
     iconCls="icon-search" closed="true" maximizable="false" minimizable="false"
     collapsible="false">
    <iframe scrolling="yes" frameborder="0" id="deptAddFrame" height="100%" width="100%"
            scrolling="No" frameborder="0"></iframe>
</div>

<div id="deptActWin" class="easyui-window" title="部门弹出信息" style="padding:5px;float:left;top:10px;"
     iconCls="icon-search" closed="true" minimizable="false" maximizable="false" maximized="true"
     collapsible="false">
    <iframe frameborder="0" id="deptActFrame" height="100%" width="100%" scrolling="No"
            frameborder="0"></iframe>
</div>
</body>
</html>